<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partInventoryManagerPartCostAdjustIndex"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <!-- <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <el-button size="small">下载</el-button>
        <el-button size="small">导入</el-button>
      </template>-->
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <editPage :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getPartCostAdjustData } from '@/api/part/inventoryManager/partCostAdjust';
// import { getStorageList } from '@/api/part/partInventory/adjustInventory/index';
import { getStorageList } from '@/api/part/common';
import editPage from './editPage';

export default {
  name: 'partCostAdjust',
  components: {
    editPage
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: {
        xhrAbort: true,
        // sort: '',
        // order: 'asc',
        limit: '10'
        // offset: '0'
      },
      visible: false,
      dialogTitle: '',
      fetchApi: getPartCostAdjustData,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      dataSource: []
    };
  },
  created() {
    this.getlist();
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          placeholder: '请选择...',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NO',
          showOverflowTooltip: true,
          placeholder: '请输入...'
          // rules: [{ required: true, message: '请输入配件代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME',
          showOverflowTooltip: true,
          placeholder: '请输入...'
          // rules: [{ required: true, message: '请输入配件代码', trigger: 'blur' }]
        }
        // {
        //   type: 'INPUT',
        //   label: '拼音代码',
        //   fieldName: 'SPELL_CODE',
        //   placeholder: '请输入...'
        //   // rules: [{ required: true, message: '请输入拼音代码', trigger: 'blur' }]
        // }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '仓库',
          dataIndex: 'STORAGE_CODE',
          editable: true,
          defaultEditable: true,
          editType: 'select',
          editItems: [],
          editRequired: 'false',
          disabled: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '库位代码',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '库存数量',
          dataIndex: 'STOCK_QUANTITY',
          sorter: true,
          filter: true,
          precision: 2,
          filterType: 'number'
        },
        {
          title: '含税成本单价',
          dataIndex: 'COST_PRICE',
          sorter: true,
          filter: true,
          hidden: false,
          precision: 2,
          filterType: 'number'
        },
        {
          title: '含税成本金额',
          dataIndex: 'COST_AMOUNT',
          sorter: true,
          filter: true,
          precision: 2,
          filterType: 'number'
        },
        {
          title: '含税销售价',
          // dataIndex: 'SALES_PRICE',
          dataIndex: 'COST_SALES_PRICE',
          sorter: true,
          filter: true,
          precision: 2,
          hidden: true,
          filterType: 'number'
        }
      ];
    },
    // 获取数据字典数据
    async getlist() {
      const storageData = (await getStorageList({})).data;
      this.topFilterList[0].itemList = storageData.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = storageData.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').filterItems = storageData.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `配件成本价${this.conf[this.handleType]}`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams.xhrAbort = true;
        this.fetchParams.xhrAbort = false;
        // this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
